#include "SOC_GX.h"
#include "rtwtypes.h"
#include <math.h>
#include "SOC_LookUp.h"
#include "Timer_Delay_Check_CRC_SOC.h"
#include "rt_nonfinite.h"
#include "SOC_GX_private.h"
#include "calPrms.h"
#include "plook_u32s16u16n16_binca_s.h"
#include "plook_u32u8u16n16_binca_s.h"
#include "intrp2d_u16u32u16n16la_se.h"
#include "look2_is16flftf_binlcapw.h"
#include "look2_ifs16lftu16Dfdf_binlcapw.h"
#include "look1_is16lftf_binlcapw.h"

#define SOC_GX_IN_Err                  ((uint8_T)1U)
#define SOC_GX_IN_NO_ACTIVE_CHILD      ((uint8_T)0U)
#define SOC_GX_IN_Name                 ((uint8_T)2U)

#define SOC_GX_IN_Full_CAL_flg         ((uint8_T)1U)
#define SOC_GX_IN_SOC_CAL_0            ((uint8_T)2U)
#define SOC_GX_IN_SOC_CAL_10           ((uint8_T)3U)
#define SOC_GX_IN_SOC_CAL_5            ((uint8_T)4U)
#define SOC_GX_IN_SOC_CAL_95           ((uint8_T)5U)
#define SOC_GX_IN_SOC_CAL_99           ((uint8_T)6U)
#define SOC_GX_IN_SOC_CAL_InI          ((uint8_T)7U)

#define SOC_GX_IN_OCV_OFF              ((uint8_T)1U)
#define SOC_GX_IN_OCV_ON               ((uint8_T)2U)

static void SOC_GX_enter_atomic_SOC_CAL_5(B_SOC_GX_c_T *localB, DW_SOC_GX_f_T
  *localDW);
static void SOC_GX_enter_atomic_SOC_CAL_0(B_SOC_GX_c_T *localB);
static void SOC_GX_enter_atomic_SOC_CAL_95(B_SOC_GX_c_T *localB, DW_SOC_GX_f_T
  *localDW);
static void SOC_G_enter_atomic_Full_CAL_flg(B_SOC_GX_c_T *localB);
static void SOC_GX_enter_atomic_SOC_CAL_99(B_SOC_GX_c_T *localB, DW_SOC_GX_f_T
  *localDW);
static void SOC_GX_enter_atomic_SOC_CAL_10(B_SOC_GX_c_T *localB, DW_SOC_GX_f_T
  *localDW);
static void SOC_GX_Static_OCV(const boolean_T *Static_Correct, const uint8_T
  Write_RealSOC_MaxMin_Flg[2], B_SOC_GX_c_T *localB);
void Alarm_nonRecov_sys_soc_cellT_Valid_Init(boolean_T *rty_Alarm_flg)
{
  *rty_Alarm_flg = false;
}

void Alarm_nonRecov_sys_soc_cellT_Valid_Reset(boolean_T *rty_Alarm_flg,
  DW_Alarm_nonRecov_sys_soc_cel_T *localDW)
{
  localDW->is_active_c14_SOC_GX = 0U;
  localDW->is_c14_SOC_GX = SOC_GX_IN_NO_ACTIVE_CHILD;
  localDW->count = 0U;
  *rty_Alarm_flg = false;
}

void Alarm_nonRecov_sys_soc_cellT_Valid(const boolean_T *rtu_Err_flg, uint16_T
  rtu_Alarm_time, boolean_T *rty_Alarm_flg, DW_Alarm_nonRecov_sys_soc_cel_T
  *localDW)
{
  if (localDW->is_active_c14_SOC_GX == 0U) {
    localDW->is_active_c14_SOC_GX = 1U;
    localDW->is_c14_SOC_GX = SOC_GX_IN_Name;
    *rty_Alarm_flg = false;
    localDW->count = 1U;
  } else if (localDW->is_c14_SOC_GX == SOC_GX_IN_Err) {
    *rty_Alarm_flg = true;
  } else {
    boolean_T out;
    *rty_Alarm_flg = false;
    out = ((*rtu_Err_flg) && (localDW->count >= rtu_Alarm_time));
    if (out) {
      localDW->is_c14_SOC_GX = SOC_GX_IN_Err;
      *rty_Alarm_flg = true;
    } else if (*rtu_Err_flg) {
      if (localDW->count >= rtu_Alarm_time) {
        localDW->count = rtu_Alarm_time;
      } else {
        localDW->count++;
      }
    } else {
      localDW->count = 1U;
    }
  }
}

static void SOC_GX_enter_atomic_SOC_CAL_5(B_SOC_GX_c_T *localB, DW_SOC_GX_f_T
  *localDW)
{
  localB->SOC_5_Keep[0] = true;
  localB->SOC_5_Keep[1] = true;
  localB->SOC_95_Keep[0] = false;
  localB->SOC_95_Keep[1] = false;
  localB->SOC_99_Keep[0] = false;
  localB->SOC_99_Keep[1] = false;
  localB->Full_Flg_Keep = false;
  localB->SOC_10_Keep[0] = false;
  localB->SOC_10_Keep[1] = false;
  localB->SOC_0_Keep[0] = false;
  localB->SOC_0_Keep[1] = false;
  localB->SOC_5_Reset[0] = true;
  localB->SOC_5_Reset[1] = true;
  localDW->T_p = 0U;
}

static void SOC_GX_enter_atomic_SOC_CAL_0(B_SOC_GX_c_T *localB)
{
  localB->SOC_0_Keep[0] = true;
  localB->SOC_0_Keep[1] = true;
  localB->SOC_5_Keep[0] = false;
  localB->SOC_5_Keep[1] = false;
  localB->SOC_95_Keep[0] = false;
  localB->SOC_95_Keep[1] = false;
  localB->SOC_99_Keep[0] = false;
  localB->SOC_99_Keep[1] = false;
  localB->Full_Flg_Keep = false;
  localB->SOC_10_Keep[0] = false;
  localB->SOC_10_Keep[1] = false;
  localB->SOC_0_Reset[0] = true;
  localB->SOC_0_Reset[1] = true;
}

static void SOC_GX_enter_atomic_SOC_CAL_95(B_SOC_GX_c_T *localB, DW_SOC_GX_f_T
  *localDW)
{
  localB->SOC_95_Reset[0] = true;
  localB->SOC_95_Reset[1] = true;
  localB->SOC_95_Keep[0] = true;
  localB->SOC_95_Keep[1] = true;
  localB->SOC_99_Keep[0] = false;
  localB->SOC_99_Keep[1] = false;
  localB->Full_Flg_Keep = false;
  localB->SOC_10_Keep[0] = false;
  localB->SOC_10_Keep[1] = false;
  localB->SOC_5_Keep[0] = false;
  localB->SOC_5_Keep[1] = false;
  localB->SOC_0_Keep[0] = false;
  localB->SOC_0_Keep[1] = false;
  localDW->T_p = 0U;
}

static void SOC_G_enter_atomic_Full_CAL_flg(B_SOC_GX_c_T *localB)
{
  localB->Full_Flg_Keep = true;
  localB->SOC_95_Keep[0] = false;
  localB->SOC_95_Keep[1] = false;
  localB->SOC_99_Keep[0] = false;
  localB->SOC_99_Keep[1] = false;
  localB->SOC_10_Keep[0] = false;
  localB->SOC_10_Keep[1] = false;
  localB->SOC_5_Keep[0] = false;
  localB->SOC_5_Keep[1] = false;
  localB->SOC_0_Keep[0] = false;
  localB->SOC_0_Keep[1] = false;
}

static void SOC_GX_enter_atomic_SOC_CAL_99(B_SOC_GX_c_T *localB, DW_SOC_GX_f_T
  *localDW)
{
  localB->SOC_99_Reset[0] = true;
  localB->SOC_99_Reset[1] = true;
  localB->SOC_99_Keep[0] = true;
  localB->SOC_99_Keep[1] = true;
  localB->SOC_95_Keep[0] = false;
  localB->SOC_95_Keep[1] = false;
  localB->Full_Flg_Keep = false;
  localB->SOC_10_Keep[0] = false;
  localB->SOC_10_Keep[1] = false;
  localB->SOC_5_Keep[0] = false;
  localB->SOC_5_Keep[1] = false;
  localB->SOC_0_Keep[0] = false;
  localB->SOC_0_Keep[1] = false;
  localDW->T_p = 0U;
}

static void SOC_GX_enter_atomic_SOC_CAL_10(B_SOC_GX_c_T *localB, DW_SOC_GX_f_T
  *localDW)
{
  localB->SOC_10_Keep[0] = true;
  localB->SOC_10_Keep[1] = true;
  localB->SOC_10_Reset[0] = true;
  localB->SOC_10_Reset[1] = true;
  localB->SOC_95_Keep[0] = false;
  localB->SOC_95_Keep[1] = false;
  localB->SOC_99_Keep[0] = false;
  localB->SOC_99_Keep[1] = false;
  localB->Full_Flg_Keep = false;
  localB->SOC_5_Keep[0] = false;
  localB->SOC_5_Keep[1] = false;
  localB->SOC_0_Keep[0] = false;
  localB->SOC_0_Keep[1] = false;
  localDW->T_p = 0U;
}

static void SOC_GX_Static_OCV(const boolean_T *Static_Correct, const uint8_T
  Write_RealSOC_MaxMin_Flg[2], B_SOC_GX_c_T *localB)
{
  if (*Static_Correct) {
    localB->Full_Flg_Keep = false;
    localB->SOC_99_Keep[0] = false;
    localB->SOC_99_Keep[1] = false;
    localB->SOC_95_Keep[0] = false;
    localB->SOC_95_Keep[1] = false;
    localB->SOC_10_Keep[0] = false;
    localB->SOC_10_Keep[1] = false;
    localB->SOC_5_Keep[0] = false;
    localB->SOC_5_Keep[1] = false;
    localB->SOC_0_Keep[0] = false;
    localB->SOC_0_Keep[1] = false;
    localB->SOC_99_Reset[0] = false;
    localB->SOC_99_Reset[1] = false;
    localB->SOC_95_Reset[0] = false;
    localB->SOC_95_Reset[1] = false;
    localB->SOC_10_Reset[0] = false;
    localB->SOC_10_Reset[1] = false;
    localB->SOC_5_Reset[0] = false;
    localB->SOC_5_Reset[1] = false;
    localB->SOC_0_Reset[0] = false;
    localB->SOC_0_Reset[1] = false;
  }

  if (Write_RealSOC_MaxMin_Flg[0] != 0) {
    localB->Full_Flg_Keep = false;
    localB->SOC_99_Keep[0] = false;
    localB->SOC_95_Keep[0] = false;
    localB->SOC_10_Keep[0] = false;
    localB->SOC_5_Keep[0] = false;
    localB->SOC_0_Keep[0] = false;
    localB->SOC_99_Reset[0] = false;
    localB->SOC_95_Reset[0] = false;
    localB->SOC_10_Reset[0] = false;
    localB->SOC_5_Reset[0] = false;
    localB->SOC_0_Reset[0] = false;
  }

  if (Write_RealSOC_MaxMin_Flg[1] != 0) {
    localB->Full_Flg_Keep = false;
    localB->SOC_99_Keep[1] = false;
    localB->SOC_95_Keep[1] = false;
    localB->SOC_10_Keep[1] = false;
    localB->SOC_5_Keep[1] = false;
    localB->SOC_0_Keep[1] = false;
    localB->SOC_99_Reset[1] = false;
    localB->SOC_95_Reset[1] = false;
    localB->SOC_10_Reset[1] = false;
    localB->SOC_5_Reset[1] = false;
    localB->SOC_0_Reset[1] = false;
  }
}

void SOC_GX_Init(DW_SOC_GX_f_T *localDW)
{
  boolean_T rtb_Alarm_flg;
  localDW->UnitDelay1_DSTATE = 1.0F;
  localDW->UnitDelay16_DSTATE[0] = SOC_CAL_DC;
  localDW->UnitDelay16_DSTATE[1] = SOC_CAL_DC;
  Alarm_nonRecov_sys_soc_cellT_Valid_Init(&rtb_Alarm_flg);
}

void SOC_GX_Disable(B_SOC_GX_c_T *localB, DW_SOC_GX_f_T *localDW)
{
  if (localDW->DiscreteIntegrator1_MODE) {
    localB->Sum1 = 0U;
    localDW->DiscreteIntegrator1_MODE = false;
  }

  localDW->Subsystem2_MODE = false;
}

void SOC_GX(const uint32_T *rtu_System_Time_Current, const uint32_T
            *rtu_System_Time_Recored, const uint8_T *rtu_INI_Flag, const
            boolean_T *rtu_SOC_Correct_Flag, const boolean_T *rtu_RTC_Reset_Flag,
            const uint8_T *rtu_FullChrg_Flg_Read, const boolean_T
            *rtu_RTC_Time_Flag, const real32_T *rtu_Count_UseSOC, const real32_T
            *rtu_RealSOC_EE_Max, const real32_T *rtu_ActSOC_EE_Min, const
            uint16_T *rtu_CellV_min, const uint16_T *rtu_CellV_max, const
            boolean_T *rtu_CellTmax_Valid, const int16_T *rtu_CellT_mean, const
            uint8_T *rtu_Fullchrg_Flag, const boolean_T *rtu_Flag_Chrg, const
            uint8_T *rtu_writeSOCDisp_flg, const uint8_T *rtu_writeSOCMax_flg,
            const uint8_T *rtu_writeSOCMin_flg, const real32_T *rtu_writeSOCDisp,
            const real32_T *rtu_writeSOCMax, const real32_T *rtu_writeSOCMin,
            const real32_T *rtu_RptSOC_EE, const int32_T *rtu_Current_mA, const
            int16_T *rtu_CellT_min, const int16_T *rtu_CellT_min_Read, const
            real32_T *rtu_SOH, const boolean_T *rtu_AP, const boolean_T
            *rtu_CellV_Valid, real32_T *rty_Real_SOC_Max, real32_T
            *rty_Real_SOC_Min, real32_T *rty_Act_SOC, real32_T *rty_Rpt_SOC,
            real32_T *rty_UseSOC, boolean_T *rty_Swich_ONOFF, real32_T
            *rty_Factor_Cap, uint8_T *rty_R_Rpt, uint32_T *rty_Ah_IN, uint8_T
            *rty_Factor_SOC95_keep, uint8_T *rty_DB_OCV_Condition, B_SOC_GX_c_T *
            localB, DW_SOC_GX_f_T *localDW)
{
  boolean_T rtb_Alarm_flg;
  int32_T rtb_LogicalOperator9_tmp;
  real32_T rtb_SOC_Compensation[2];
  real32_T rtb_Add;
  real32_T rtb_Divide4_f;
  real32_T rtb_Divide8;
  real32_T rtb_SOP_Pack_PulseDischrg;
  real32_T rtb_Saturation4_k_idx_0;
  real32_T rtb_Saturation4_k_idx_1;
  real32_T rtb_VectorConcatenate1_idx_0;
  real32_T rtb_VectorConcatenate1_idx_1;
  uint32_T bpIndices[2];
  uint32_T bpIndices_0[2];
  uint32_T rtb_UnitDelay;
  uint16_T fractions[2];
  uint16_T fractions_0[2];
  uint16_T rtb_DCV_idx_0;
  uint16_T rtb_DCV_idx_1;
  uint16_T rtb_Keep_CellV;
  uint8_T Write_RealSOC_MaxMin_Flg[2];
  uint8_T rtb_Switch10_m;
  boolean_T LogicalOperator;
  boolean_T LogicalOperator1;
  boolean_T Static_Correct;
  boolean_T Z;
  boolean_T rtb_Compare_cg;
  boolean_T rtb_LogicalOperator11_g_tmp;
  boolean_T rtb_LogicalOperator11_o;
  boolean_T rtb_LogicalOperator14;
  boolean_T rtb_LogicalOperator16_h;
  boolean_T rtb_LogicalOperator2;
  boolean_T rtb_LogicalOperator5;
  boolean_T rtb_LogicalOperator5_j;
  boolean_T rtb_LogicalOperator9;
  boolean_T rtb_NVM_Err_Cal;
  boolean_T rtb_Pseudo_Static_Cal;
  boolean_T rtb_RTC_Reset_Cal;
  boolean_T rtb_RelationalOperator19;
  boolean_T rtb_RelationalOperator31;
  boolean_T rtb_RelationalOperator4;
  boolean_T rtb_RelationalOperator_p;
  rtb_LogicalOperator16_h = (*rtu_FullChrg_Flg_Read != 0);
  rtb_RelationalOperator_p = (*rtu_Fullchrg_Flag == 1);
  if (localDW->UnitDelay20_DSTATE[0] > SOC_CAL) {
    rtb_Saturation4_k_idx_0 = SOC_CAL;
  } else if (localDW->UnitDelay20_DSTATE[0] < 0.0F) {
    rtb_Saturation4_k_idx_0 = 0.0F;
  } else {
    rtb_Saturation4_k_idx_0 = localDW->UnitDelay20_DSTATE[0];
  }

  if (localDW->UnitDelay20_DSTATE[1] > SOC_CAL) {
    rtb_Saturation4_k_idx_1 = SOC_CAL;
  } else if (localDW->UnitDelay20_DSTATE[1] < 0.0F) {
    rtb_Saturation4_k_idx_1 = 0.0F;
  } else {
    rtb_Saturation4_k_idx_1 = localDW->UnitDelay20_DSTATE[1];
  }

  if (localDW->is_active_c7_SOC_GX == 0U) {
    localDW->is_active_c7_SOC_GX = 1U;
    localB->SOC_Correct = *rtu_SOC_Correct_Flag;
    localDW->T_l = 0U;
  } else {
    if (*rtu_SOC_Correct_Flag) {
      localB->SOC_Correct = 1.0F;
    }

    if ((localDW->T_l < 1010) && (*rtu_CellTmax_Valid)) {
      localDW->T_l++;
    }

    if (localDW->T_l >= 1010) {
      localB->SOC_Correct = 0.0F;
    }
  }

  rtb_RTC_Reset_Cal = ((*rtu_RTC_Reset_Flag) && (!rtb_LogicalOperator16_h) &&
                       (localB->SOC_Correct != 0.0F));
  rtb_LogicalOperator5 = ((*rtu_INI_Flag != 0) && (localB->SOC_Correct != 0.0F));
  if (rtb_LogicalOperator5) {
    rtb_UnitDelay = *rtu_System_Time_Current - *rtu_System_Time_Recored;
  } else {
    rtb_UnitDelay = 0U;
  }

  rtb_LogicalOperator5 = ((*rtu_RTC_Time_Flag) && (rtb_UnitDelay > Correct_Time)
    && (!rtb_LogicalOperator16_h) && (*rtu_INI_Flag != 0) &&
    (localB->SOC_Correct != 0.0F));
  rtb_LogicalOperator5_j = (*rtu_Count_UseSOC > correct_CountSOC);
  if (*rtu_Current_mA < 0) {
    rtb_LogicalOperator9_tmp = -*rtu_Current_mA;
  } else {
    rtb_LogicalOperator9_tmp = *rtu_Current_mA;
  }

  rtb_LogicalOperator9 = ((rtb_LogicalOperator9_tmp < 3000) && (*rtu_INI_Flag !=
    0));
  if (rtb_LogicalOperator9) {
    if (!localDW->DiscreteIntegrator1_MODE) {
      localDW->UnitDelay_DSTATE_d = 0U;
      localDW->DiscreteIntegrator1_MODE = true;
    }

    localB->Sum1 = localDW->UnitDelay_DSTATE_d + 1U;
    if (localDW->UnitDelay_DSTATE_d + 1U < 1U) {
      localB->Sum1 = MAX_uint32_T;
    }

    localDW->UnitDelay_DSTATE_d = localB->Sum1;
  } else if (localDW->DiscreteIntegrator1_MODE) {
    localB->Sum1 = 0U;
    localDW->DiscreteIntegrator1_MODE = false;
  }

  rtb_LogicalOperator9 = (localB->Sum1 >= 6000U);
  rtb_Pseudo_Static_Cal = (rtb_LogicalOperator5_j && (!rtb_LogicalOperator16_h) &&
    (*rtu_RTC_Time_Flag) && ((rtb_UnitDelay > correct_CountSOC_Time) ||
    (rtb_LogicalOperator9 && (!localDW->UnitDelay_DSTATE_o))));
  rtb_VectorConcatenate1_idx_0 = *rtu_RealSOC_EE_Max;
  rtb_VectorConcatenate1_idx_1 = *rtu_ActSOC_EE_Min;
  rtb_NVM_Err_Cal = (((fmaxf(rtb_VectorConcatenate1_idx_0,
    rtb_VectorConcatenate1_idx_1) > SOC_CAL) || (fminf
    (rtb_VectorConcatenate1_idx_0, rtb_VectorConcatenate1_idx_1) <= 0.0F)) &&
                     (*rtu_INI_Flag != 0) && (localB->SOC_Correct != 0.0F));
  rtb_RelationalOperator4 = (*rtu_CellV_min >= 2000);
  rtb_LogicalOperator16_h = (*rtu_CellV_max < OCV_Correct_CellV);
  rtb_LogicalOperator5_j = (*rtu_CellT_min > 10);
  if (*rtu_CellTmax_Valid) {
    if (!localDW->Subsystem2_MODE) {
      Alarm_nonRecov_sys_soc_cellT_Valid_Reset(&rtb_Alarm_flg,
        &localDW->sf_Chart3);
      localDW->Subsystem2_MODE = true;
    }

    Alarm_nonRecov_sys_soc_cellT_Valid(rtu_CellTmax_Valid, 1000, &rtb_Alarm_flg,
      &localDW->sf_Chart3);
    localB->OutportBufferForAlarm_flg = rtb_Alarm_flg;
  } else {
    localDW->Subsystem2_MODE = false;
  }

  rtb_RelationalOperator4 = ((rtb_RTC_Reset_Cal || rtb_LogicalOperator5 ||
    rtb_Pseudo_Static_Cal || rtb_NVM_Err_Cal) && rtb_RelationalOperator4 &&
    rtb_LogicalOperator16_h && rtb_LogicalOperator5_j &&
    localB->OutportBufferForAlarm_flg);
  *rty_Swich_ONOFF = (rtb_RelationalOperator4 && (!localDW->UnitDelay21_DSTATE_f));
  rtb_LogicalOperator5_j = (rtb_RelationalOperator_p || (*rty_Swich_ONOFF));
  localDW->UnitDelay6_DSTATE = (rtb_LogicalOperator5_j ||
    localDW->UnitDelay6_DSTATE);
  if (*rtu_writeSOCMax_flg != 0) {
    localDW->UnitDelay3_DSTATE_h = 1U;
  }

  rtb_SOC_Compensation[0] = localDW->UnitDelay20_DSTATE_d[0] -
    rtb_VectorConcatenate1_idx_0;
  rtb_SOC_Compensation[1] = localDW->UnitDelay20_DSTATE_d[1] -
    rtb_VectorConcatenate1_idx_1;
  rtb_LogicalOperator16_h = (rtb_RelationalOperator_p && (*rtu_Flag_Chrg));
  if (((!*rty_Swich_ONOFF) && localDW->UnitDelay17_DSTATE) ||
      rtb_LogicalOperator16_h) {
    localDW->UnitDelay22_DSTATE = false;
  } else {
    localDW->UnitDelay22_DSTATE = (((localB->SOC_Correct == 1.0F) &&
      (localDW->UnitDelay21_DSTATE == 0.0F)) || localDW->UnitDelay22_DSTATE);
  }

  if (!localDW->UnitDelay22_DSTATE) {
    localDW->UnitDelay16_DSTATE_i[1] = *rtu_CellV_min;
    localDW->UnitDelay16_DSTATE_i[0] = *rtu_CellV_max;
  }

  SOC_LookUp(*rty_Swich_ONOFF, rtb_SOC_Compensation,
             localDW->UnitDelay16_DSTATE_i, rtu_CellT_min, localB->Calib_RealSOC,
             localB->Lookup_RealSOC, &localB->Subtract);
  rtb_LogicalOperator16_h = (((fabsf(localDW->UnitDelay20_DSTATE_d[0] -
    localB->Calib_RealSOC[0]) > 10.0F) || (fabsf(localDW->UnitDelay20_DSTATE_d[1]
    - localB->Calib_RealSOC[1]) > 10.0F)) && rtb_Pseudo_Static_Cal &&
    (*rty_Swich_ONOFF));
  if (rtb_LogicalOperator16_h) {
    localDW->UnitDelay3_DSTATE[0] = localDW->UnitDelay20_DSTATE_d[0] * 0.7F +
      localB->Calib_RealSOC[0] * 0.3F;
    localDW->UnitDelay3_DSTATE[1] = localDW->UnitDelay20_DSTATE_d[1] * 0.7F +
      localB->Calib_RealSOC[1] * 0.3F;
  }

  localDW->UnitDelay2_DSTATE_a = (rtb_LogicalOperator16_h ||
    localDW->UnitDelay2_DSTATE_a);
  localDW->UnitDelay11_DSTATE_e = ((*rty_Swich_ONOFF) ||
    localDW->UnitDelay11_DSTATE_e);
  rtb_Compare_cg = ((fmaxf(localB->Lookup_RealSOC[0], localB->Lookup_RealSOC[1])
                     <= 25.0F) && (fminf(localB->Lookup_RealSOC[0],
    localB->Lookup_RealSOC[1]) >= 0.0F) && localDW->UnitDelay11_DSTATE_e);
  if (rtb_Compare_cg) {
    if (localDW->UnitDelay2_DSTATE_a) {
      localDW->UnitDelay20_DSTATE_d[0] = localDW->UnitDelay3_DSTATE[0];
      localDW->UnitDelay20_DSTATE_d[1] = localDW->UnitDelay3_DSTATE[1];
    } else {
      localDW->UnitDelay20_DSTATE_d[0] = localB->Calib_RealSOC[0];
      localDW->UnitDelay20_DSTATE_d[1] = localB->Calib_RealSOC[1];
    }
  } else {
    localDW->UnitDelay20_DSTATE_d[0] = rtb_VectorConcatenate1_idx_0;
    localDW->UnitDelay20_DSTATE_d[1] = rtb_VectorConcatenate1_idx_1;
  }

  if (localDW->UnitDelay20_DSTATE_d[0] > SOC_CAL) {
    localDW->UnitDelay20_DSTATE_d[0] = SOC_CAL;
  } else if (localDW->UnitDelay20_DSTATE_d[0] < 0.0F) {
    localDW->UnitDelay20_DSTATE_d[0] = 0.0F;
  }

  if (localDW->UnitDelay20_DSTATE_d[1] > SOC_CAL) {
    localDW->UnitDelay20_DSTATE_d[1] = SOC_CAL;
  } else if (localDW->UnitDelay20_DSTATE_d[1] < 0.0F) {
    localDW->UnitDelay20_DSTATE_d[1] = 0.0F;
  }

  if (localDW->UnitDelay3_DSTATE_h != 0) {
    if (*rtu_writeSOCMax > 101.0F) {
      rtb_SOC_Compensation[0] = 101.0F;
    } else if (*rtu_writeSOCMax < 0.0F) {
      rtb_SOC_Compensation[0] = 0.0F;
    } else {
      rtb_SOC_Compensation[0] = *rtu_writeSOCMax;
    }
  } else {
    rtb_SOC_Compensation[0] = localDW->UnitDelay20_DSTATE_d[0];
  }

  if (*rtu_writeSOCMin_flg != 0) {
    localDW->UnitDelay4_DSTATE = 1U;
  }

  if (localDW->UnitDelay4_DSTATE != 0) {
    if (*rtu_writeSOCMin > 101.0F) {
      rtb_SOC_Compensation[1] = 101.0F;
    } else if (*rtu_writeSOCMin < 0.0F) {
      rtb_SOC_Compensation[1] = 0.0F;
    } else {
      rtb_SOC_Compensation[1] = *rtu_writeSOCMin;
    }
  } else {
    rtb_SOC_Compensation[1] = localDW->UnitDelay20_DSTATE_d[1];
  }

  if (rtb_LogicalOperator5_j) {
    localDW->UnitDelay5_DSTATE = rtb_Saturation4_k_idx_0;
  } else if (!localDW->UnitDelay6_DSTATE) {
    localDW->UnitDelay5_DSTATE = rtb_SOC_Compensation[0];
  }

  rtb_Add = rtb_Saturation4_k_idx_0 - localDW->UnitDelay5_DSTATE;
  Static_Correct = ((*rty_Swich_ONOFF) && rtb_Compare_cg);
  LogicalOperator1 = ((*rtu_Flag_Chrg) && rtb_RelationalOperator_p);
  Write_RealSOC_MaxMin_Flg[0] = *rtu_writeSOCMax_flg;
  Write_RealSOC_MaxMin_Flg[1] = *rtu_writeSOCMin_flg;
  bpIndices[0U] = plook_u32s16u16n16_binca_s(*rtu_CellT_min, (const int16_T *)
    &CCV_CellT[0], 5U, &rtb_Keep_CellV);
  fractions[0U] = rtb_Keep_CellV;
  bpIndices[1U] = plook_u32u8u16n16_binca_s(95U, (const uint8_T *)&CCV_SOC[0],
    2U, &rtb_Keep_CellV);
  fractions[1U] = rtb_Keep_CellV;
  rtb_DCV_idx_0 = intrp2d_u16u32u16n16la_se(bpIndices, fractions, (const
    uint16_T *)&CCV_Vol[0], 6U, rtCP_CCV_maxIndex);
  bpIndices[1U] = plook_u32u8u16n16_binca_s(99U, (const uint8_T *)&CCV_SOC[0],
    2U, &rtb_Keep_CellV);
  fractions[1U] = rtb_Keep_CellV;
  plook_u32u8u16n16_binca_s(100U, (const uint8_T *)&CCV_SOC[0], 2U,
    &rtb_Keep_CellV);
  rtb_Compare_cg = (*rtu_CellV_max > rtb_DCV_idx_0);
  rtb_Divide4_f = (real32_T)rtb_LogicalOperator9_tmp / SOC_CAP / 1000.0F;
  LogicalOperator = ((*rtu_Flag_Chrg) && (*rtu_Current_mA <= -1000));
  rtb_LogicalOperator16_h = (rtb_Compare_cg && (rtb_Divide4_f < 0.33F) &&
    LogicalOperator && (localDW->UnitDelay19_DSTATE < 95.0F) &&
    (*rtu_CellV_Valid));
  Timer_Delay_Check_CRC_SOC(rtb_LogicalOperator16_h, !rtb_LogicalOperator16_h,
    SOC_95_ConfirmTime, SOC_95_RcvTime, &rtb_RelationalOperator_p,
    &localDW->Timer_Delay_Check_CRC_SOC_h);
  rtb_Compare_cg = (*rtu_CellV_max > intrp2d_u16u32u16n16la_se(bpIndices,
    fractions, (const uint16_T *)&CCV_Vol[0], 6U, rtCP_CCV_maxIndex));
  rtb_LogicalOperator2 = (rtb_Compare_cg && (rtb_Divide4_f < 0.33F) &&
    LogicalOperator && (localDW->UnitDelay19_DSTATE < 99.0F) &&
    (*rtu_CellV_Valid));
  Timer_Delay_Check_CRC_SOC(rtb_LogicalOperator2, !rtb_LogicalOperator2,
    SOC_99_ConfirmTime, SOC_99_RcvTime, &rtb_LogicalOperator16_h,
    &localDW->Timer_Delay_Check_CRC_SOC1);
  bpIndices_0[0U] = plook_u32s16u16n16_binca_s(*rtu_CellT_min, (const int16_T *)
    &DCV_CellT[0], 9U, &rtb_Keep_CellV);
  fractions_0[0U] = rtb_Keep_CellV;
  bpIndices_0[1U] = plook_u32u8u16n16_binca_s(0U, (const uint8_T *)&DCV_SOC[0],
    2U, &rtb_Keep_CellV);
  fractions_0[1U] = rtb_Keep_CellV;
  rtb_DCV_idx_0 = intrp2d_u16u32u16n16la_se(bpIndices_0, fractions_0, (const
    uint16_T *)&DCV_Vol[0], 10U, rtCP_DCV_maxIndex);
  bpIndices_0[1U] = plook_u32u8u16n16_binca_s(5U, (const uint8_T *)&DCV_SOC[0],
    2U, &rtb_Keep_CellV);
  fractions_0[1U] = rtb_Keep_CellV;
  rtb_DCV_idx_1 = intrp2d_u16u32u16n16la_se(bpIndices_0, fractions_0, (const
    uint16_T *)&DCV_Vol[0], 10U, rtCP_DCV_maxIndex);
  bpIndices_0[1U] = plook_u32u8u16n16_binca_s(10U, (const uint8_T *)&DCV_SOC[0],
    2U, &rtb_Keep_CellV);
  fractions_0[1U] = rtb_Keep_CellV;
  rtb_Compare_cg = (*rtu_CellV_min < intrp2d_u16u32u16n16la_se(bpIndices_0,
    fractions_0, (const uint16_T *)&DCV_Vol[0], 10U, rtCP_DCV_maxIndex));
  rtb_LogicalOperator2 = (*rtu_CellV_min > 1500);
  rtb_LogicalOperator11_g_tmp = !LogicalOperator;
  rtb_LogicalOperator11_o = (rtb_Compare_cg && (rtb_Divide4_f < 0.5F) &&
    rtb_LogicalOperator11_g_tmp && (localDW->UnitDelay19_DSTATE > 10.0F) &&
    (*rtu_CellV_Valid) && rtb_LogicalOperator2);
  Timer_Delay_Check_CRC_SOC(rtb_LogicalOperator11_o, !rtb_LogicalOperator11_o,
    SOC_10_ConfirmTime, SOC_10_RcvTime, &rtb_LogicalOperator2,
    &localDW->Timer_Delay_Check_CRC_SOC2);
  rtb_Compare_cg = (*rtu_CellV_min < rtb_DCV_idx_1);
  rtb_LogicalOperator11_o = (*rtu_CellV_min > 1500);
  rtb_LogicalOperator14 = (rtb_Compare_cg && (rtb_Divide4_f < 0.5F) &&
    rtb_LogicalOperator11_g_tmp && (localDW->UnitDelay19_DSTATE > 5.0F) &&
    (*rtu_CellV_Valid) && rtb_LogicalOperator11_o);
  Timer_Delay_Check_CRC_SOC(rtb_LogicalOperator14, !rtb_LogicalOperator14,
    SOC_10_ConfirmTime, SOC_10_RcvTime, &rtb_LogicalOperator11_o,
    &localDW->Timer_Delay_Check_CRC_SOC3);
  rtb_Compare_cg = (*rtu_CellV_min <= rtb_DCV_idx_0);
  rtb_LogicalOperator14 = (*rtu_CellV_min > 1500);
  rtb_LogicalOperator14 = (rtb_Compare_cg && rtb_LogicalOperator11_g_tmp &&
    (localDW->UnitDelay19_DSTATE > 0.0F) && (*rtu_CellV_Valid) &&
    rtb_LogicalOperator14);
  Timer_Delay_Check_CRC_SOC(rtb_LogicalOperator14, !rtb_LogicalOperator14,
    SOC_0_ConfirmTime, SOC_0_RcvTime, &Z, &localDW->Timer_Delay_Check_CRC_SOC4);
  if (localDW->is_active_c4_SOC_GX == 0U) {
    localDW->is_active_c4_SOC_GX = 1U;
    localB->Full_Flg_Keep = false;
    localB->SOC_99_Keep[0] = false;
    localB->SOC_99_Keep[1] = false;
    localB->SOC_95_Keep[0] = false;
    localB->SOC_95_Keep[1] = false;
    localB->SOC_10_Keep[0] = false;
    localB->SOC_10_Keep[1] = false;
    localB->SOC_5_Keep[0] = false;
    localB->SOC_5_Keep[1] = false;
    localB->SOC_0_Keep[0] = false;
    localB->SOC_0_Keep[1] = false;
    localB->SOC_99_Reset[0] = false;
    localB->SOC_99_Reset[1] = false;
    localB->SOC_95_Reset[0] = false;
    localB->SOC_95_Reset[1] = false;
    localB->SOC_10_Reset[0] = false;
    localB->SOC_10_Reset[1] = false;
    localB->SOC_5_Reset[0] = false;
    localB->SOC_5_Reset[1] = false;
    localB->SOC_0_Reset[0] = false;
    localB->SOC_0_Reset[1] = false;
    localDW->is_c4_SOC_GX = SOC_GX_IN_SOC_CAL_InI;
  } else {
    switch (localDW->is_c4_SOC_GX) {
     case SOC_GX_IN_Full_CAL_flg:
      if (!LogicalOperator) {
        localDW->is_c4_SOC_GX = SOC_GX_IN_SOC_CAL_InI;
      }
      break;

     case SOC_GX_IN_SOC_CAL_0:
      if (LogicalOperator) {
        localDW->is_c4_SOC_GX = SOC_GX_IN_SOC_CAL_InI;
      } else if (rtb_LogicalOperator11_o && (!Z) && (localDW->T_p >= 4000)) {
        localDW->is_c4_SOC_GX = SOC_GX_IN_SOC_CAL_5;
        SOC_GX_enter_atomic_SOC_CAL_5(localB, localDW);
      } else {
        localB->SOC_0_Reset[0] = false;
        localB->SOC_0_Reset[1] = false;
        if (Static_Correct || (Write_RealSOC_MaxMin_Flg[0] != 0) ||
            (Write_RealSOC_MaxMin_Flg[1] != 0)) {
          SOC_GX_Static_OCV(&Static_Correct, Write_RealSOC_MaxMin_Flg, localB);
        }
      }
      break;

     case SOC_GX_IN_SOC_CAL_10:
      if (rtb_LogicalOperator11_o && (localDW->T_p >= 4000)) {
        localDW->is_c4_SOC_GX = SOC_GX_IN_SOC_CAL_5;
        SOC_GX_enter_atomic_SOC_CAL_5(localB, localDW);
      } else if (LogicalOperator) {
        localDW->is_c4_SOC_GX = SOC_GX_IN_SOC_CAL_InI;
      } else if ((localDW->T_p >= 4000) && rtb_LogicalOperator2) {
        localDW->is_c4_SOC_GX = SOC_GX_IN_SOC_CAL_10;
        SOC_GX_enter_atomic_SOC_CAL_10(localB, localDW);
      } else {
        localB->SOC_10_Reset[0] = false;
        localB->SOC_10_Reset[1] = false;
        rtb_LogicalOperator9_tmp = localDW->T_p + 1;
        if (localDW->T_p + 1 > 65535) {
          rtb_LogicalOperator9_tmp = 65535;
        }

        localDW->T_p = (uint16_T)rtb_LogicalOperator9_tmp;
        if (Static_Correct || (Write_RealSOC_MaxMin_Flg[0] != 0) ||
            (Write_RealSOC_MaxMin_Flg[1] != 0)) {
          SOC_GX_Static_OCV(&Static_Correct, Write_RealSOC_MaxMin_Flg, localB);
        }
      }
      break;

     case SOC_GX_IN_SOC_CAL_5:
      if (Z) {
        localDW->is_c4_SOC_GX = SOC_GX_IN_SOC_CAL_0;
        SOC_GX_enter_atomic_SOC_CAL_0(localB);
      } else if (LogicalOperator) {
        localDW->is_c4_SOC_GX = SOC_GX_IN_SOC_CAL_InI;
      } else if ((localDW->T_p >= 4000) && rtb_LogicalOperator11_o) {
        localDW->is_c4_SOC_GX = SOC_GX_IN_SOC_CAL_5;
        SOC_GX_enter_atomic_SOC_CAL_5(localB, localDW);
      } else if (rtb_LogicalOperator2 && (!rtb_LogicalOperator11_o) &&
                 (localDW->T_p >= 4000)) {
        localDW->is_c4_SOC_GX = SOC_GX_IN_SOC_CAL_10;
        SOC_GX_enter_atomic_SOC_CAL_10(localB, localDW);
      } else {
        localB->SOC_5_Reset[0] = false;
        localB->SOC_5_Reset[1] = false;
        rtb_LogicalOperator9_tmp = localDW->T_p + 1;
        if (localDW->T_p + 1 > 65535) {
          rtb_LogicalOperator9_tmp = 65535;
        }

        localDW->T_p = (uint16_T)rtb_LogicalOperator9_tmp;
        if (Static_Correct || (Write_RealSOC_MaxMin_Flg[0] != 0) ||
            (Write_RealSOC_MaxMin_Flg[1] != 0)) {
          SOC_GX_Static_OCV(&Static_Correct, Write_RealSOC_MaxMin_Flg, localB);
        }
      }
      break;

     case SOC_GX_IN_SOC_CAL_95:
      if ((localDW->T_p >= 500) && rtb_RelationalOperator_p) {
        localDW->is_c4_SOC_GX = SOC_GX_IN_SOC_CAL_95;
        SOC_GX_enter_atomic_SOC_CAL_95(localB, localDW);
      } else if (LogicalOperator1) {
        localDW->is_c4_SOC_GX = SOC_GX_IN_Full_CAL_flg;
        SOC_G_enter_atomic_Full_CAL_flg(localB);
      } else if (rtb_LogicalOperator16_h && (localDW->T_p >= 500)) {
        localDW->is_c4_SOC_GX = SOC_GX_IN_SOC_CAL_99;
        SOC_GX_enter_atomic_SOC_CAL_99(localB, localDW);
      } else if (!LogicalOperator) {
        localDW->is_c4_SOC_GX = SOC_GX_IN_SOC_CAL_InI;
      } else {
        localB->SOC_95_Reset[0] = false;
        localB->SOC_95_Reset[1] = false;
        rtb_LogicalOperator9_tmp = localDW->T_p + 1;
        if (localDW->T_p + 1 > 65535) {
          rtb_LogicalOperator9_tmp = 65535;
        }

        localDW->T_p = (uint16_T)rtb_LogicalOperator9_tmp;
        if (Static_Correct || (Write_RealSOC_MaxMin_Flg[0] != 0) ||
            (Write_RealSOC_MaxMin_Flg[1] != 0)) {
          SOC_GX_Static_OCV(&Static_Correct, Write_RealSOC_MaxMin_Flg, localB);
        }
      }
      break;

     case SOC_GX_IN_SOC_CAL_99:
      if (LogicalOperator1) {
        localDW->is_c4_SOC_GX = SOC_GX_IN_Full_CAL_flg;
        SOC_G_enter_atomic_Full_CAL_flg(localB);
      } else if (!LogicalOperator) {
        localDW->is_c4_SOC_GX = SOC_GX_IN_SOC_CAL_InI;
      } else if ((localDW->T_p >= 500) && rtb_LogicalOperator16_h) {
        localDW->is_c4_SOC_GX = SOC_GX_IN_SOC_CAL_99;
        SOC_GX_enter_atomic_SOC_CAL_99(localB, localDW);
      } else if ((!rtb_LogicalOperator16_h) && rtb_RelationalOperator_p &&
                 (localDW->T_p >= 500)) {
        localDW->is_c4_SOC_GX = SOC_GX_IN_SOC_CAL_95;
        SOC_GX_enter_atomic_SOC_CAL_95(localB, localDW);
      } else {
        localB->SOC_99_Reset[0] = false;
        localB->SOC_99_Reset[1] = false;
        rtb_LogicalOperator9_tmp = localDW->T_p + 1;
        if (localDW->T_p + 1 > 65535) {
          rtb_LogicalOperator9_tmp = 65535;
        }

        localDW->T_p = (uint16_T)rtb_LogicalOperator9_tmp;
        if (Static_Correct || (Write_RealSOC_MaxMin_Flg[0] != 0) ||
            (Write_RealSOC_MaxMin_Flg[1] != 0)) {
          SOC_GX_Static_OCV(&Static_Correct, Write_RealSOC_MaxMin_Flg, localB);
        }
      }
      break;

     default:
      if (LogicalOperator1) {
        localDW->is_c4_SOC_GX = SOC_GX_IN_Full_CAL_flg;
        SOC_G_enter_atomic_Full_CAL_flg(localB);
      } else if (rtb_LogicalOperator16_h && LogicalOperator) {
        localDW->is_c4_SOC_GX = SOC_GX_IN_SOC_CAL_99;
        SOC_GX_enter_atomic_SOC_CAL_99(localB, localDW);
      } else if (rtb_RelationalOperator_p && LogicalOperator) {
        localDW->is_c4_SOC_GX = SOC_GX_IN_SOC_CAL_95;
        SOC_GX_enter_atomic_SOC_CAL_95(localB, localDW);
      } else if (Z && rtb_LogicalOperator11_g_tmp) {
        localDW->is_c4_SOC_GX = SOC_GX_IN_SOC_CAL_0;
        SOC_GX_enter_atomic_SOC_CAL_0(localB);
      } else if (rtb_LogicalOperator11_o && rtb_LogicalOperator11_g_tmp) {
        localDW->is_c4_SOC_GX = SOC_GX_IN_SOC_CAL_5;
        SOC_GX_enter_atomic_SOC_CAL_5(localB, localDW);
      } else if (rtb_LogicalOperator2) {
        localDW->is_c4_SOC_GX = SOC_GX_IN_SOC_CAL_10;
        SOC_GX_enter_atomic_SOC_CAL_10(localB, localDW);
      } else if (Static_Correct || (Write_RealSOC_MaxMin_Flg[0] != 0) ||
                 (Write_RealSOC_MaxMin_Flg[1] != 0)) {
        SOC_GX_Static_OCV(&Static_Correct, Write_RealSOC_MaxMin_Flg, localB);
      }
      break;
    }
  }

  rtb_Compare_cg = (*rtu_CellV_min > rtb_DCV_idx_0);
  rtb_RelationalOperator31 = (*rtu_Current_mA > 20);
  rtb_RelationalOperator19 = (*rtu_Current_mA > 20);
  rtb_LogicalOperator14 = ((localDW->UnitDelay20_DSTATE[0] <= 1.0F) &&
    rtb_Compare_cg && rtb_RelationalOperator31);
  Z = ((localDW->UnitDelay20_DSTATE[0] < 0.0F) && rtb_RelationalOperator19);
  rtb_Compare_cg = ((localDW->UnitDelay20_DSTATE[1] <= 1.0F) && rtb_Compare_cg &&
                    rtb_RelationalOperator31);
  rtb_RelationalOperator19 = ((localDW->UnitDelay20_DSTATE[1] < 0.0F) &&
    rtb_RelationalOperator19);
  rtb_SOP_Pack_PulseDischrg = look2_is16flftf_binlcapw(*rtu_CellT_mean,
    rtb_Divide4_f, (const int16_T *)&CellC_T[0], (const real32_T *)&CellC_C[0],
    (const real32_T *)&Cell_Capacity[0], rtCP_SOP_Pack_PulseDischrg_maxI, 11U);
  rtb_Keep_CellV = look2_ifs16lftu16Dfdf_binlcapw(rtb_Divide4_f, *rtu_CellT_min,
    (const real32_T *)&SOC_Keep_C[0], (const int16_T *)&SOC_Keep_T[0], (const
    uint16_T *)&SOC_Keep_Vol[0], rtCP_Keep_CellV_maxIndex, 5U);
  if (!LogicalOperator) {
    localDW->UnitDelay14_DSTATE_h = false;
  } else {
    LogicalOperator = (*rtu_CellV_max > rtb_Keep_CellV);
    localDW->UnitDelay14_DSTATE_h = (LogicalOperator ||
      localDW->UnitDelay14_DSTATE_h);
  }

  if (rtb_LogicalOperator11_g_tmp || localDW->UnitDelay14_DSTATE_h) {
    localDW->UnitDelay1_DSTATE = 1.0F;
  } else {
    LogicalOperator = (*rtu_CellV_max < rtb_Keep_CellV);
    if (LogicalOperator && (localDW->UnitDelay19_DSTATE > 95.0F)) {
      localDW->UnitDelay1_DSTATE = 0.1F;
    }
  }

  rtb_Divide8 = (real32_T)*rtu_Current_mA * 0.01F * rtb_SOP_Pack_PulseDischrg /
    36000.0F * localDW->UnitDelay1_DSTATE;
  localDW->UnitDelay20_DSTATE[0] = localDW->UnitDelay_DSTATE[0];
  Static_Correct = (Static_Correct || LogicalOperator1);
  if (Static_Correct || (Write_RealSOC_MaxMin_Flg[0] != 0) ||
      localB->SOC_95_Reset[0] || localB->SOC_99_Reset[0] || localB->
      SOC_10_Reset[0] || localB->SOC_5_Reset[0] || localB->SOC_0_Reset[0]) {
    localDW->UnitDelay_DSTATE[0] = 0.0F;
  } else {
    if (rtb_LogicalOperator14 || Z) {
      rtb_Divide4_f = 0.0F;
    } else {
      rtb_Divide4_f = rtb_Divide8;
    }

    localDW->UnitDelay_DSTATE[0] = rtb_Divide4_f + localDW->UnitDelay20_DSTATE[0];
  }

  localDW->UnitDelay20_DSTATE[1] = localDW->UnitDelay_DSTATE[1];
  if (Static_Correct || (Write_RealSOC_MaxMin_Flg[1] != 0) ||
      localB->SOC_95_Reset[1] || localB->SOC_99_Reset[1] || localB->
      SOC_10_Reset[1] || localB->SOC_5_Reset[1] || localB->SOC_0_Reset[1]) {
    localDW->UnitDelay_DSTATE[1] = 0.0F;
  } else {
    if (rtb_Compare_cg || rtb_RelationalOperator19) {
      rtb_Divide8 = 0.0F;
    }

    localDW->UnitDelay_DSTATE[1] = rtb_Divide8 + localDW->UnitDelay20_DSTATE[1];
  }

  rtb_Divide4_f = floorf(localDW->UnitDelay_DSTATE[0] + 25000.0F);
  if (rtIsNaNF(rtb_Divide4_f) || rtIsInfF(rtb_Divide4_f)) {
    rtb_Divide4_f = 0.0F;
  } else {
    rtb_Divide4_f = fmodf(rtb_Divide4_f, 4.2949673E+9F);
  }

  *rty_Ah_IN = rtb_Divide4_f < 0.0F ? (uint32_T)-(int32_T)(uint32_T)
    -rtb_Divide4_f : (uint32_T)rtb_Divide4_f;
  if (rtb_LogicalOperator5_j) {
    localDW->UnitDelay14_DSTATE = 0.0F;
  } else {
    localDW->UnitDelay14_DSTATE += fabsf(rtb_Add - localDW->UnitDelay13_DSTATE);
  }

  if (localDW->UnitDelay6_DSTATE) {
    rtb_Divide4_f = 0.0F;
  } else {
    rtb_Divide4_f = *rtu_Count_UseSOC;
  }

  *rty_UseSOC = localDW->UnitDelay14_DSTATE + rtb_Divide4_f;
  rtb_Divide4_f = floorf(localDW->UnitDelay1_DSTATE * 10.0F);
  if (rtIsNaNF(rtb_Divide4_f) || rtIsInfF(rtb_Divide4_f)) {
    rtb_Divide4_f = 0.0F;
  } else {
    rtb_Divide4_f = fmodf(rtb_Divide4_f, 256.0F);
  }

  *rty_Factor_SOC95_keep = (uint8_T)(rtb_Divide4_f < 0.0F ? (int32_T)(uint8_T)
    -(int8_T)(uint8_T)-rtb_Divide4_f : (int32_T)(uint8_T)rtb_Divide4_f);
  rtb_Divide4_f = floorf(localDW->UnitDelay8_DSTATE * 10.0F);
  if (rtIsNaNF(rtb_Divide4_f) || rtIsInfF(rtb_Divide4_f)) {
    rtb_Divide4_f = 0.0F;
  } else {
    rtb_Divide4_f = fmodf(rtb_Divide4_f, 256.0F);
  }

  *rty_R_Rpt = (uint8_T)(rtb_Divide4_f < 0.0F ? (int32_T)(uint8_T)-(int8_T)
    (uint8_T)-rtb_Divide4_f : (int32_T)(uint8_T)rtb_Divide4_f);
  if (localB->SOC_95_Reset[0] || ((!localDW->UnitDelay4_DSTATE_e) &&
       rtb_RelationalOperator_p)) {
    localDW->UnitDelay5_DSTATE_m = rtb_Saturation4_k_idx_0;
  }

  if (localB->SOC_99_Reset[0] || ((!localDW->UnitDelay6_DSTATE_p) &&
       rtb_LogicalOperator16_h)) {
    localDW->UnitDelay7_DSTATE = rtb_Saturation4_k_idx_0;
  }

  if (localB->SOC_10_Reset[1] || ((!localDW->UnitDelay9_DSTATE_a) &&
       rtb_LogicalOperator2)) {
    localDW->UnitDelay10_DSTATE = rtb_Saturation4_k_idx_1;
  }

  if (localB->SOC_5_Reset[1] || ((!localDW->UnitDelay13_DSTATE_i) &&
       rtb_LogicalOperator11_o)) {
    localDW->UnitDelay11_DSTATE = rtb_Saturation4_k_idx_1;
  }

  if (localDW->UnitDelay11_DSTATE_e) {
    rtb_VectorConcatenate1_idx_1 = localB->Subtract;
  } else {
    rtb_VectorConcatenate1_idx_1 = rtb_VectorConcatenate1_idx_0 -
      rtb_VectorConcatenate1_idx_1;
  }

  if ((localDW->UnitDelay11_DSTATE - 5.0F >= 0.0F) &&
      (localDW->UnitDelay11_DSTATE - 5.0F <= 10.0F) && rtb_LogicalOperator11_o)
  {
    localDW->UnitDelay12_DSTATE = 5.0F;
  } else if ((localDW->UnitDelay11_DSTATE - 5.0F > 10.0F) &&
             rtb_LogicalOperator11_o) {
    localDW->UnitDelay12_DSTATE = localDW->UnitDelay11_DSTATE - 10.0F;
  }

  if ((localDW->UnitDelay10_DSTATE - 10.0F <= 10.0F) && rtb_LogicalOperator2) {
    localDW->UnitDelay8_DSTATE_f = 10.0F;
  } else if ((localDW->UnitDelay10_DSTATE - 10.0F > 10.0F) &&
             rtb_LogicalOperator2) {
    localDW->UnitDelay8_DSTATE_f = localDW->UnitDelay10_DSTATE - 10.0F;
  }

  if ((95.0F - localDW->UnitDelay5_DSTATE_m <= 10.0F) &&
      rtb_RelationalOperator_p) {
    localDW->UnitDelay2_DSTATE = 95U;
  } else {
    if ((95.0F - localDW->UnitDelay5_DSTATE_m > 10.0F) &&
        rtb_RelationalOperator_p) {
      rtb_Divide4_f = localDW->UnitDelay5_DSTATE_m + 10.0F;
    } else {
      rtb_Divide4_f = localDW->UnitDelay2_DSTATE;
    }

    rtb_Divide4_f = floorf(rtb_Divide4_f);
    if (rtIsNaNF(rtb_Divide4_f) || rtIsInfF(rtb_Divide4_f)) {
      rtb_Divide4_f = 0.0F;
    } else {
      rtb_Divide4_f = fmodf(rtb_Divide4_f, 65536.0F);
    }

    localDW->UnitDelay2_DSTATE = (uint16_T)(rtb_Divide4_f < 0.0F ? (int32_T)
      (uint16_T)-(int16_T)(uint16_T)-rtb_Divide4_f : (int32_T)(uint16_T)
      rtb_Divide4_f);
  }

  if ((99.0F - localDW->UnitDelay7_DSTATE <= 10.0F) && rtb_LogicalOperator16_h)
  {
    localDW->UnitDelay3_DSTATE_p = 99U;
  } else {
    if ((99.0F - localDW->UnitDelay7_DSTATE > 10.0F) && rtb_LogicalOperator16_h)
    {
      rtb_Divide4_f = localDW->UnitDelay7_DSTATE + 10.0F;
    } else {
      rtb_Divide4_f = localDW->UnitDelay3_DSTATE_p;
    }

    rtb_Divide4_f = floorf(rtb_Divide4_f);
    if (rtIsNaNF(rtb_Divide4_f) || rtIsInfF(rtb_Divide4_f)) {
      rtb_Divide4_f = 0.0F;
    } else {
      rtb_Divide4_f = fmodf(rtb_Divide4_f, 65536.0F);
    }

    localDW->UnitDelay3_DSTATE_p = (uint16_T)(rtb_Divide4_f < 0.0F ? (int32_T)
      (uint16_T)-(int16_T)(uint16_T)-rtb_Divide4_f : (int32_T)(uint16_T)
      rtb_Divide4_f);
  }

  rtb_VectorConcatenate1_idx_0 = 0.01F * *rtu_SOH;
  rtb_Divide8 = rtb_VectorConcatenate1_idx_0 * SOC_CAP;
  localDW->UnitDelay19_DSTATE = rtb_Saturation4_k_idx_1 / ((SOC_CAL +
    rtb_Saturation4_k_idx_1) - rtb_Saturation4_k_idx_0) * 100.0F;
  if (localDW->UnitDelay19_DSTATE > 100.0F) {
    localDW->UnitDelay19_DSTATE = 100.0F;
  } else if (localDW->UnitDelay19_DSTATE < 0.0F) {
    localDW->UnitDelay19_DSTATE = 0.0F;
  }

  if (*rtu_CellTmax_Valid) {
    *rty_Factor_Cap = look1_is16lftf_binlcapw(*rtu_CellT_min, (const int16_T *)
      &Capacity_Keep_CellT[0], (const real32_T *)&Capacity_Keep_Factor[0], 8U);
  } else {
    *rty_Factor_Cap = look1_is16lftf_binlcapw(*rtu_CellT_min_Read, (const
      int16_T *)&Capacity_Keep_CellT[0], (const real32_T *)
      &Capacity_Keep_Factor[0], 8U);
  }

  rtb_Divide4_f = (localDW->UnitDelay19_DSTATE - (100.0F - *rty_Factor_Cap *
    100.0F)) / *rty_Factor_Cap;
  if (rtb_Divide4_f > 100.0F) {
    *rty_Act_SOC = 100.0F;
  } else if (rtb_Divide4_f < 0.0F) {
    *rty_Act_SOC = 0.0F;
  } else {
    *rty_Act_SOC = rtb_Divide4_f;
  }

  if (LogicalOperator1) {
    if (*rtu_AP) {
      rtb_Divide4_f = SOC_CAL_DC;
    } else {
      rtb_Divide4_f = SOC_CAL;
    }

    localDW->UnitDelay16_DSTATE[1] = rtb_Divide4_f -
      rtb_VectorConcatenate1_idx_1;
    localDW->UnitDelay16_DSTATE[0] = rtb_Divide4_f;
  }

  if (localB->Full_Flg_Keep) {
    localDW->UnitDelay20_DSTATE_d[0] = localDW->UnitDelay16_DSTATE[0];
    localDW->UnitDelay20_DSTATE_d[1] = localDW->UnitDelay16_DSTATE[1];
  } else {
    real32_T tmp;
    localDW->UnitDelay20_DSTATE_d[1] = localDW->UnitDelay8_DSTATE_f;
    localDW->UnitDelay20_DSTATE_d[0] = rtb_VectorConcatenate1_idx_1 +
      localDW->UnitDelay8_DSTATE_f;
    rtb_Divide4_f = floorf(rtb_VectorConcatenate1_idx_1);
    if (rtIsNaNF(rtb_Divide4_f) || rtIsInfF(rtb_Divide4_f)) {
      tmp = 0.0F;
      rtb_Divide4_f = 0.0F;
    } else {
      tmp = fmodf(rtb_Divide4_f, 65536.0F);
      rtb_Divide4_f = fmodf(rtb_Divide4_f, 65536.0F);
    }

    if (localB->SOC_99_Keep[0]) {
      localDW->UnitDelay20_DSTATE_d[0] = localDW->UnitDelay3_DSTATE_p;
    } else if (localB->SOC_95_Keep[0]) {
      localDW->UnitDelay20_DSTATE_d[0] = localDW->UnitDelay2_DSTATE;
    } else if (!localB->SOC_10_Keep[0]) {
      if (localB->SOC_5_Keep[0]) {
        localDW->UnitDelay20_DSTATE_d[0] = rtb_VectorConcatenate1_idx_1 +
          localDW->UnitDelay12_DSTATE;
      } else if (localB->SOC_0_Keep[0]) {
        localDW->UnitDelay20_DSTATE_d[0] = rtb_VectorConcatenate1_idx_1;
      } else {
        localDW->UnitDelay20_DSTATE_d[0] = rtb_SOC_Compensation[0];
      }
    }

    if (localB->SOC_99_Keep[1]) {
      localDW->UnitDelay20_DSTATE_d[1] = (uint16_T)(localDW->UnitDelay3_DSTATE_p
        - (uint32_T)(rtb_Divide4_f < 0.0F ? (int32_T)(uint16_T)-(int16_T)
                     (uint16_T)-rtb_Divide4_f : (int32_T)(uint16_T)rtb_Divide4_f));
    } else if (localB->SOC_95_Keep[1]) {
      localDW->UnitDelay20_DSTATE_d[1] = (uint16_T)(localDW->UnitDelay2_DSTATE -
        (uint32_T)(tmp < 0.0F ? (int32_T)(uint16_T)-(int16_T)(uint16_T)-tmp :
                   (int32_T)(uint16_T)tmp));
    } else if (!localB->SOC_10_Keep[1]) {
      if (localB->SOC_5_Keep[1]) {
        localDW->UnitDelay20_DSTATE_d[1] = localDW->UnitDelay12_DSTATE;
      } else if (localB->SOC_0_Keep[1]) {
        localDW->UnitDelay20_DSTATE_d[1] = 0.0F;
      } else {
        localDW->UnitDelay20_DSTATE_d[1] = rtb_SOC_Compensation[1];
      }
    }
  }

  localDW->UnitDelay20_DSTATE_d[0] -= localDW->UnitDelay_DSTATE[0] / rtb_Divide8;
  localDW->UnitDelay20_DSTATE_d[1] -= localDW->UnitDelay_DSTATE[1] / rtb_Divide8;
  rtb_VectorConcatenate1_idx_1 = (real32_T)(*rty_Act_SOC - (real_T)SOC_OffSet);
  rtb_LogicalOperator5_j = (*rtu_writeSOCDisp_flg == 0);
  rtb_Divide8 = (real32_T)(SOC_CAL - (real_T)SOC_OffSet) * SOC_Factor;
  if (LogicalOperator1 || ((localDW->UnitDelay25_DSTATE >= rtb_Divide8) &&
       (*rtu_Current_mA < 0))) {
    Static_Correct = false;
  } else {
    Static_Correct = (((localDW->UnitDelay12_DSTATE_l < 0.0F) &&
                       (*rtu_Current_mA > 20)) || localDW->UnitDelay11_DSTATE_a);
  }

  localDW->UnitDelay11_DSTATE_a = (rtb_LogicalOperator5_j && Static_Correct);
  rtb_LogicalOperator5_j = (*rtu_writeSOCDisp_flg == 0);
  localDW->UnitDelay10_DSTATE_d = (rtb_LogicalOperator5_j && (LogicalOperator1 ||
    ((localDW->UnitDelay28_DSTATE >= rtb_Divide8) && (*rtu_Current_mA < 0)) ||
    localDW->UnitDelay10_DSTATE_d));
  if (*rtu_writeSOCDisp_flg != 0) {
    localDW->UnitDelay2_DSTATE_b = 1U;
  }

  if (localDW->UnitDelay11_DSTATE_a) {
    rtb_Divide4_f = 0.0F;
  } else if (localDW->UnitDelay10_DSTATE_d) {
    rtb_Divide4_f = rtb_Divide8;
  } else if (localDW->UnitDelay2_DSTATE_b != 0) {
    if (*rtu_writeSOCDisp > 101.0F) {
      rtb_Divide4_f = 101.0F;
    } else if (*rtu_writeSOCDisp < 0.0F) {
      rtb_Divide4_f = 0.0F;
    } else {
      rtb_Divide4_f = *rtu_writeSOCDisp;
    }
  } else {
    rtb_Divide4_f = *rtu_RptSOC_EE;
  }

  if (!localDW->UnitDelay14_DSTATE_a) {
    rtb_LogicalOperator5_j = (((*rtu_Current_mA > 20) &&
      (localDW->UnitDelay27_DSTATE < 0.0F)) || ((localDW->UnitDelay27_DSTATE >=
      rtb_Divide8) && (*rtu_Current_mA < 0)) || (*rtu_writeSOCDisp_flg != 0) ||
      LogicalOperator1);
    if (rtb_LogicalOperator5_j) {
      localDW->UnitDelay13_DSTATE_d = 0.0F;
    } else {
      localDW->UnitDelay13_DSTATE_d = rtb_SOP_Pack_PulseDischrg * (real32_T)
        *rtu_Current_mA * localDW->UnitDelay8_DSTATE * 0.01F / 36000.0F +
        localDW->UnitDelay9_DSTATE;
    }
  }

  localDW->UnitDelay27_DSTATE = (real32_T)((real_T)rtb_Divide4_f -
    localDW->UnitDelay13_DSTATE_d / SOC_CAP / DOD / rtb_VectorConcatenate1_idx_0);
  rtb_LogicalOperator5_j = (*rtu_writeSOCDisp_flg == 0);
  Static_Correct = (*rtu_writeSOCDisp_flg == 0);
  localDW->UnitDelay14_DSTATE_a = (Static_Correct && (*rtu_Current_mA < -100) &&
    (localDW->UnitDelay27_DSTATE >= 99.4F) && (localDW->UnitDelay27_DSTATE <
    100.0F) && (!LogicalOperator1));
  if ((*rtu_Current_mA > 100) || (*rtu_Current_mA < -100) ||
      (!(localDW->UnitDelay27_DSTATE > 100.0F)) || (!rtb_LogicalOperator5_j)) {
    if (LogicalOperator1 && Static_Correct) {
      localDW->UnitDelay27_DSTATE = rtb_Divide8;
    } else if (localDW->UnitDelay14_DSTATE_a) {
      localDW->UnitDelay27_DSTATE = 99.4F;
    }
  }

  if (!(rtb_VectorConcatenate1_idx_1 >= 0.0F)) {
    rtb_VectorConcatenate1_idx_1 = 0.0F;
  }

  rtb_SOP_Pack_PulseDischrg = (real32_T)((real_T)(rtb_VectorConcatenate1_idx_1 *
    SOC_Factor) - localDW->UnitDelay27_DSTATE);
  rtb_LogicalOperator5_j = (*rtu_Current_mA >= 0);
  if ((rtb_Saturation4_k_idx_0 < 30.0F) && (rtb_SOP_Pack_PulseDischrg <= -0.5F) &&
      rtb_LogicalOperator5_j) {
    localDW->UnitDelay8_DSTATE = 1.8F;
  } else if (rtb_LogicalOperator5_j) {
    if (rtb_SOP_Pack_PulseDischrg >= 3.0F) {
      localDW->UnitDelay8_DSTATE = 0.7F;
    } else if ((rtb_SOP_Pack_PulseDischrg < 3.0F) && (rtb_SOP_Pack_PulseDischrg >=
                0.5F)) {
      localDW->UnitDelay8_DSTATE = 0.9F;
    } else if (rtb_SOP_Pack_PulseDischrg <= -3.0F) {
      localDW->UnitDelay8_DSTATE = 1.5F;
    } else if ((rtb_SOP_Pack_PulseDischrg > -3.0F) && (rtb_SOP_Pack_PulseDischrg
                <= -0.5F)) {
      localDW->UnitDelay8_DSTATE = 1.3F;
    } else {
      localDW->UnitDelay8_DSTATE = 1.0F;
    }
  } else if (rtb_SOP_Pack_PulseDischrg >= 3.0F) {
    localDW->UnitDelay8_DSTATE = 1.5F;
  } else if ((rtb_SOP_Pack_PulseDischrg < 3.0F) && (rtb_SOP_Pack_PulseDischrg >=
              0.5F)) {
    localDW->UnitDelay8_DSTATE = 1.3F;
  } else if (rtb_SOP_Pack_PulseDischrg <= -3.0F) {
    localDW->UnitDelay8_DSTATE = 0.7F;
  } else if ((rtb_SOP_Pack_PulseDischrg > -3.0F) && (rtb_SOP_Pack_PulseDischrg <=
              -0.5F)) {
    localDW->UnitDelay8_DSTATE = 0.9F;
  } else {
    localDW->UnitDelay8_DSTATE = 1.0F;
  }

  if (localDW->UnitDelay27_DSTATE > 100.1F) {
    *rty_Rpt_SOC = 100.1F;
  } else if (localDW->UnitDelay27_DSTATE < 0.0F) {
    *rty_Rpt_SOC = 0.0F;
  } else {
    *rty_Rpt_SOC = localDW->UnitDelay27_DSTATE;
  }

  if (rtb_NVM_Err_Cal) {
    rtb_Switch10_m = 4U;
  } else if (rtb_Pseudo_Static_Cal) {
    rtb_Switch10_m = 3U;
  } else if (rtb_LogicalOperator5) {
    rtb_Switch10_m = 2U;
  } else {
    rtb_Switch10_m = rtb_RTC_Reset_Cal;
  }

  if (localDW->is_active_c8_SOC_GX == 0U) {
    localDW->is_active_c8_SOC_GX = 1U;
    localDW->is_c8_SOC_GX = SOC_GX_IN_OCV_OFF;
    *rty_DB_OCV_Condition = rtb_Switch10_m;
  } else if (localDW->is_c8_SOC_GX == SOC_GX_IN_OCV_OFF) {
    if (rtb_Switch10_m > 0) {
      localDW->is_c8_SOC_GX = SOC_GX_IN_OCV_ON;
      localDW->T = 0U;
      *rty_DB_OCV_Condition = rtb_Switch10_m;
    }
  } else if (localDW->T >= 600) {
    localDW->is_c8_SOC_GX = SOC_GX_IN_OCV_OFF;
    *rty_DB_OCV_Condition = rtb_Switch10_m;
  } else {
    localDW->T++;
  }

  *rty_Real_SOC_Max = rtb_Saturation4_k_idx_0;
  *rty_Real_SOC_Min = rtb_Saturation4_k_idx_1;
  localDW->UnitDelay20_DSTATE[0] = localDW->UnitDelay20_DSTATE_d[0];
  localDW->UnitDelay20_DSTATE[1] = localDW->UnitDelay20_DSTATE_d[1];
  localDW->UnitDelay_DSTATE_o = rtb_LogicalOperator9;
  localDW->UnitDelay21_DSTATE_f = rtb_RelationalOperator4;
  localDW->UnitDelay17_DSTATE = *rty_Swich_ONOFF;
  localDW->UnitDelay21_DSTATE = localB->SOC_Correct;
  localDW->UnitDelay13_DSTATE = rtb_Add;
  localDW->UnitDelay4_DSTATE_e = rtb_RelationalOperator_p;
  localDW->UnitDelay6_DSTATE_p = rtb_LogicalOperator16_h;
  localDW->UnitDelay9_DSTATE_a = rtb_LogicalOperator2;
  localDW->UnitDelay13_DSTATE_i = rtb_LogicalOperator11_o;
  localDW->UnitDelay9_DSTATE = localDW->UnitDelay13_DSTATE_d;
  localDW->UnitDelay25_DSTATE = localDW->UnitDelay27_DSTATE;
  localDW->UnitDelay12_DSTATE_l = localDW->UnitDelay27_DSTATE;
  localDW->UnitDelay28_DSTATE = localDW->UnitDelay27_DSTATE;
}

void SOC_GX_initialize(const char_T **rt_errorStatus, RT_MODEL_SOC_GX_T *const
  SOC_GX_M)
{
  rt_InitInfAndNaN(sizeof(real_T));
  rtmSetErrorStatusPointer(SOC_GX_M, rt_errorStatus);
}
